#$Source: Volumes://MATEOR/prlserverapi/www/default.pl$
#$Revision: 0.01-0a$
#$Date: June 19, 2012$
#
#----------------
# PrlServerAPI Copyright Matthew Caswell 2012
#----------------
#

use strict;

my($Revision) = '0.01-0a';

my $cont;
my $blank = <<EOF;
<!-- [INSERT: BLANK CODE] -->
EOF

%Default_pl = (
  'content' => sub{
    my($r, $port, $host, $dr, $ip) = @_;
    my $content = <<EOF;
<!DOCTYPE html>
<html>
  <head>
    <title>PrlServerAPI $Revision server</title>
    <script language=javascript>
      <!--
      //Browser Support Code
      function xmlhttpPost(strURL, elementId){
        var xmlhttp;
        var self = this;
        var oDiv = document.getElementById(elementId);

        try{
          // Opera 8.0+, Mozilla, Safari
          self.xmlhttp = new XMLHttpRequest();
        }catch(e){
          // MS-IE :(
          try{
            self.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
          }catch(e){
            try{
              self.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }catch(e){
              // Unsupported browser or browser doesn't support AJAX
              
            }
          }
        }
        self.xmlhttp.open('GET', strURL, true);
        self.xmlhttp.setRequestHeader('Content-Type', 'text/html');
        self.xmlhttp.onreadystatechange = function(){
          if(self.xmlhttp.readyState == 4){
            updatepage(self.xmlhttp.responseText, oDiv);
          }
        }
        self.xmlhttp.send();
      }
      function updatepage(str, div){
        div.innerHTML = str;
      }
      //-->
    </script>
    <style type="text/css">
      div#content{
        float:left;
        border-style:outset;
        border-width:2px;
        border-color:blue;
        margin-left:15%;
        width:900px;
        padding:0px;
        background-color:black;
        color:#f0f0f0;
        text-align:center;
      }
      div.clear{
        clear:both;
      }
      div#navbar{
        border-style:solid;
        border-width:1px;
        border-color:red;
      }
      div#navbar a{
        text-decoration:none;
        color:#f0f0f0;
      }
      div#footer{
        border-style:solid;
        border-width:1px;
        border-color:purple;
        margin-top:90%;
        height:20px;
        text-align:center;
      }
      div.column{
        color:#f0f0f0;
        border-style:solid;
        border-width:1px;
        float:left;
      }
      div.cell{
        color:#f0f0f0;
        border-style:solid;
        border-width:1px;
      }
    </style>
  </head>
  <body bgcolor="#A9A9A9">
    <div id="content">
      <h1>Welcome to $host!</h1>
      <div id="navbar">
        <a href="JavaScript:xmlhttpPost('/default.pl?show.infotable', 'infotable');">Information Table</a> . <a href="http://code.google.com/p/prlserverapi/">PrlServerAPI</a> . <a href="$ip">Index page</a>
      </div>
      <div id="data">
        <p>PrlServerAPI is a research project, creating an easy to use API for TCP/IP programming and communication in Perl.</p>
        <div id="infotable"></div>
      </div>
      <div id="footer">
        PrlServerAPI $Revision &copy; Copyright Matthew Caswell 2012
      </div>
    </div>
    <div class="clear" />
  </body>
</html>
EOF

    my $infotable = <<EOF;
<!-- [START: ?infotable] -->
          <h3>Info about this server</h3>
          <div class="column">
            <div class="cell">
              Port
            </div>
            <div class="cell">
              Hostname
            </div>
            <div class="cell">
              API Rev
            </div>
            <div class="cell">
              default redirect
            </div>
            <div class="cell">
              index path
            </div>
          </div>
          <div class="column">
            <div class="cell">
              $port
            </div>
            <div class="cell">
              $host
            </div>
            <div class="cell">
              $Revision
            </div> 
            <div class="cell">
              $dr
            </div>
            <div class="cell">
              $ip
            </div>
          </div>
          <a href="JavaScript:xmlhttpPost('/default.pl?hide.infotable', 'infotable');">[ close ]</a>
<!-- [END: ?infotable] -->
EOF
    my $it2 = <<eof;
<!-- [START: ?infotable] -->
          <h3>Info about this server</h3>
          <div class="column">
            <div class="cell">
              Port
            </div>
            <div class="cell">
              Hostname
            </div>
            <div class="cell">
              API Rev
            </div>
            <div class="cell">
              default redirect
            </div>
            <div class="cell">
              index path
            </div>
          </div>
          <div class="column">
            <div class="cell">
              $port
            </div>
            <div class="cell">
              $host
            </div>
            <div class="cell">
              $Revision
            </div> 
            <div class="cell">
              $dr
            </div>
            <div class="cell">
              $ip
            </div>
          </div>
<!-- [END: ?infotable] -->
eof
    if($r->{uri}->{params}->{'show.infotable'} != 0 and $r->{uri}->{params}->{'hide.infotable'} == 0){
      $cont = $infotable;
    }elsif($r->{uri}->{params}->{'show.infotable'} == 0 and $r->{uri}->{params}->{'hide.infotable'} != 0){
      $cont = $blank;
    }elsif($r->{uri}->{params}->{'store.infotable'} != 0){
      open FILE, ">$r->{uri}->{params}->{'location'}"
        or print "Couldn't store info table!: $!\n";
      print FILE $it2;
      close FILE;
      $cont = "xml.0";
    }else{
      $cont = $content;
    }

    return $cont;
  },
);
